package com.cgbl.cg.ui.mine

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.WorksCollectionAdapter
import com.cgbl.cg.adapter.WorksCollectionAdapter2
import com.cgbl.cg.databinding.AppActivityOtherPersonalPageBinding
import com.cgbl.cg.databinding.AppActivityPersonalPageBinding
import com.cgbl.cg.view.CommonConfirmPop
import com.cgbl.cg.view.CreateFolderPop
import com.cgbl.cg.vm.mine.OtherPersonalPageViewModel
import com.cgbl.cg.vm.mine.PersonalPageViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CollectDetailBean
import com.robot.base.model.WorksBean
import com.robot.base.util.arouter.ARouterUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener


@Route(path = AppConstants.Router.Mine.A_MINE_OTHER_PERSONAL_PAGE)
class OtherPersonalPageActivity :
    GLBaseActivity<AppActivityOtherPersonalPageBinding, OtherPersonalPageViewModel>() {

    lateinit var worksAdapter: WorksCollectionAdapter

    lateinit var collectionAdapter: WorksCollectionAdapter2

    var uid = ""

    override fun initComponents() {
        initTiterBar()
        initView()
        viewModel.fetchUidByUserIndo(uid)
    }

    private fun initView() {
        worksAdapter = WorksCollectionAdapter()
        collectionAdapter = WorksCollectionAdapter2()
        binding.recyclerView.apply {
            layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            adapter = worksAdapter
        }

        binding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.refresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
            }
        })
        collectionAdapter.setOnItemClickListener { adapter, view, position ->
            val item = collectionAdapter.data[position]
            ARouterUtil.goToActivityWithBundle(
                AppConstants.Router.COLLECTION.A_COLLECTIONS_DETAILS,
                Bundle().apply {
                    putString(
                        AppConstants.BundleKey.COLLECTION_DATA, item.collectId
                    )
                })
        }
    }

    private fun initTiterBar() {
    }


    override fun initViewObservable() {
        viewModel.uc.newWorksData.observe(this) {
            if (it.isEmpty()) {
                val emptyView = layoutInflater.inflate(R.layout.app_layout_empty, null)
                worksAdapter.setEmptyView(emptyView)
            }
            worksAdapter.setNewInstance(it as MutableList<WorksBean>?)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
        viewModel.uc.loadMoreWorksData.observe(this) {
            worksAdapter.addData(it)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
        viewModel.uc.newCollectionPageData.observe(this) {
            if (it.isEmpty()) {
                val emptyView = layoutInflater.inflate(R.layout.app_layout_empty, null)
                collectionAdapter.setEmptyView(emptyView)
            }
            collectionAdapter.setNewInstance(it as MutableList<CollectDetailBean>?)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
        viewModel.uc.loadMoreCollectionPageData.observe(this) {
            collectionAdapter.addData(it)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }

        viewModel.uc.switchTabEvent.observe(this) {
            binding.recyclerView.adapter = if (it) worksAdapter else collectionAdapter
        }
    }

    override fun initParam() {
        uid = intent.getStringExtra(AppConstants.BundleKey.ID) ?: ""
    }

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_other_personal_page

    override fun initVariableId(): Int = BR.viewModel

}